package cn.net.csit.platform_cloud.basicmodel.service.impl;

import cn.net.csit.platform_cloud.basicmodel.domain.BasicObjDictionaryDomain;
import cn.net.csit.platform_cloud.basicmodel.domain.BasicObjDictionaryDomainExample;
import cn.net.csit.platform_cloud.basicmodel.mapper.BasicObjDictionaryDomainMapper;
import cn.net.csit.platform_cloud.basicmodel.service.IBackupDataService;
import cn.net.csit.platform_cloud.basicmodel.service.IBasicObjectDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.lang.model.element.NestingKind;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName:
 * @Author:yanwenjuan
 * @Date:2019/4/811:01
 * @Description:
 */
@Service
public class BasicObjectDictionaryServiceImpl implements IBasicObjectDictionaryService {
    @Autowired
    private BasicObjDictionaryDomainMapper mapper;
    @Autowired
    IBackupDataService backupDataService;


    @Override
    public List<BasicObjDictionaryDomain> getAll() {
        BasicObjDictionaryDomainExample example=new BasicObjDictionaryDomainExample();
        BasicObjDictionaryDomainExample.Criteria criteria= example.createCriteria();
        criteria.andUuidIsNotNull();
        return mapper.selectByExample(example);
    }


    @Override
    public List<BasicObjDictionaryDomain> getAll(Map<String, String> map) {
        BasicObjDictionaryDomainExample example=new BasicObjDictionaryDomainExample();
        BasicObjDictionaryDomainExample.Criteria criteria= example.createCriteria();
        criteria.andUuidIsNotNull();
        if(null!=map.get("objName")){
            criteria.andObjNameLike("%"+map.get("objName")+"%");
        }
        List<BasicObjDictionaryDomain> listDomain=mapper.selectByExample(example);
        //遍历集合，根据父uuid查询父名称，重新赋值
        if(null!=listDomain&&listDomain.size()>0){
                for(BasicObjDictionaryDomain domain:listDomain){
                    if(null!=domain.getObjParentUuid()&&!domain.getObjParentUuid().equals("")){
                        BasicObjDictionaryDomain d=mapper.selectByPrimaryKey(domain.getObjParentUuid());
                        if(null!=d&&null!=d.getObjName()&&!d.getObjName().equals("")){
                            domain.setObjParentUuid(d.getObjName());
                        }
                    }



                }
        }
        return listDomain;
    }

    @Override
    public int insert(BasicObjDictionaryDomain record) {
        return mapper.insert(record);
    }

    @Override
    public BasicObjDictionaryDomain selectByPrimaryKey(String uuid) {
        return mapper.selectByPrimaryKey(uuid);
    }

    @Override
    public int updateByPrimaryKey(BasicObjDictionaryDomain record) {
        return mapper.updateByPrimaryKey(record);
    }

    @Override
    public int deleteByPrimaryKey(String uuid) {
        return mapper.deleteByPrimaryKey(uuid);
    }

    @Override
    public Map<String, String> getObjName(Map<String, String> map) {
        Map<String,String> result = new HashMap<>();
        String where="where  1=1 ";
        if(null!=map&null!=map.get("uuid")){
            where =where+" and bod_t0.uuid="+map.get("uuid");
        }
        StringBuilder sb=new StringBuilder();
        sb.append(" SELECT bod_t0.obj_name");
        sb.append(" FROM basic_obj_dictionary bod_t0");
        sb.append(" "+where);
        result=backupDataService.customQueryItem(sb).get(0);
        return result;
    }

    @Override
    public List<Map<String, String>> getObjParentUuid() {
        List<Map<String, String>> result = new ArrayList<>();
        StringBuilder sb = new StringBuilder();
        sb.append(" SELECT bod_t0.uuid, obj_code");
        sb.append(" FROM basic_obj_dictionary bod_t0");
        result = backupDataService.customQueryItem(sb);
        return result;
    }

    @Override
    public List<Map<String, String> >getObjPropByOrganType(Map<String, String> map) {
        List<Map<String, String> > result = new ArrayList<>();
        String where = "where 1=1";
        if(null !=map &null != map.get("code")){
            where = where+" and bod_t0.organ_type='"+map.get("code")+"'";
        }
        if(null !=map &null != map.get("uuid")){
            where = where+" and bod_t0.uuid='"+map.get("uuid")+"'";
        }
        StringBuilder sb = new StringBuilder();
        sb.append(" SELECT bod_t0.uuid,bod_t0.obj_code,bod_t0.obj_name,temp_t0.prop_uuid,temp_t0.prop_code,temp_t0.prop_name,temp_t0.prop_type,temp_t0.prop_is_multiterm ");
        sb.append(" FROM basic_obj_dictionary bod_t0 ");
        sb.append(" INNER JOIN (SELECT bopr_t0.obj_uuid,bopr_t0.prop_uuid,bpd_t0.prop_code,bpd_t0.prop_name,bpd_t0.prop_type,bpd_t0.prop_is_multiterm ");
        sb.append(" FROM basic_obj_property_relation bopr_t0 ");
        sb.append(" INNER JOIN (SELECT uuid,prop_code,prop_name,prop_type,prop_is_multiterm ");
        sb.append(" FROM basic_property_dictionary) bpd_t0 ON bpd_t0.uuid = bopr_t0.prop_uuid) temp_t0 ON bod_t0.uuid = temp_t0.obj_uuid ");
        sb.append(" "+where+"");
        result = backupDataService.customQueryItem(sb);
        return result;
    }


}
